package com.housekeeping.dao.impl;

import com.housekeeping.dao.UserDAO;
import com.housekeeping.model.User;
import com.housekeeping.utils.DBUtils;

import java.util.List;

public class UserDAOImpl implements UserDAO {

    @Override
    public User findByUsernameAndPassword(String username, String password) {
        String sql = "SELECT user_id as userId, username, password, real_name as realName, phone, email, address, " +
                    "user_type as userType, status, create_time as createTime, update_time as updateTime " +
                    "FROM users WHERE username = ? AND password = ? AND status = 1";
        return DBUtils.queryForObject(sql, User.class, username, password);
    }

    @Override
    public User findByUsername(String username) {
        String sql = "SELECT user_id as userId, username, password, real_name as realName, phone, email, address, " +
                    "user_type as userType, status, create_time as createTime, update_time as updateTime " +
                    "FROM users WHERE username = ?";
        return DBUtils.queryForObject(sql, User.class, username);
    }

    @Override
    public User findById(Integer userId) {
        String sql = "SELECT user_id as userId, username, password, real_name as realName, phone, email, address, " +
                    "user_type as userType, status, create_time as createTime, update_time as updateTime " +
                    "FROM users WHERE user_id = ?";
        return DBUtils.queryForObject(sql, User.class, userId);
    }

    @Override
    public List<User> findAll() {
        String sql = "SELECT user_id as userId, username, password, real_name as realName, phone, email, address, " +
                    "user_type as userType, status, create_time as createTime, update_time as updateTime " +
                    "FROM users ORDER BY create_time DESC";
        return DBUtils.queryForList(sql, User.class);
    }

    @Override
    public List<User> findByUserType(Integer userType) {
        String sql = "SELECT user_id as userId, username, password, real_name as realName, phone, email, address, " +
                    "user_type as userType, status, create_time as createTime, update_time as updateTime " +
                    "FROM users WHERE user_type = ? ORDER BY create_time DESC";
        return DBUtils.queryForList(sql, User.class, userType);
    }

    @Override
    public int insert(User user) {
        String sql = "INSERT INTO users (username, password, real_name, phone, email, address, user_type, status, create_time) " +
                    "VALUES (?, ?, ?, ?, ?, ?, ?, ?, NOW())";
        return DBUtils.update(sql, user.getUsername(), user.getPassword(), user.getRealName(),
                user.getPhone(), user.getEmail(), user.getAddress(), user.getUserType(), user.getStatus());
    }

    @Override
    public int update(User user) {
        String sql = "UPDATE users SET real_name = ?, phone = ?, email = ?, address = ?, user_type = ?, " +
                    "status = ?, update_time = NOW() WHERE user_id = ?";
        return DBUtils.update(sql, user.getRealName(), user.getPhone(), user.getEmail(),
                user.getAddress(), user.getUserType(), user.getStatus(), user.getUserId());
    }

    @Override
    public int delete(Integer userId) {
        String sql = "DELETE FROM users WHERE user_id = ?";
        return DBUtils.update(sql, userId);
    }

    @Override
    public long count() {
        String sql = "SELECT COUNT(*) FROM users";
        Object result = DBUtils.queryForScalar(sql);
        return result != null ? ((Number) result).longValue() : 0;
    }
} 